package br.folhapgto.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import br.folhapgto.dao.banco.Conexao;
import br.folhapgto.dao.idao.ISindicatoDAO;
import br.folhapgto.model.Sindicato;
import java.util.ArrayList;

public class SindicatoDAO implements ISindicatoDAO{
	
	private static SindicatoDAO instanciaUnica = new SindicatoDAO();

    public static SindicatoDAO getInstanciaUnica() 
    {
        return instanciaUnica;
    }

    // Construtor
    public SindicatoDAO()
    {
    	
    }
    
    // M�todos CRUD
    public Sindicato carregaDados(ResultSet rs) throws SQLException
    {
    	Sindicato sindicato = new Sindicato();
        
    	sindicato.setIdSindicato(rs.getInt("idSindicato"));
    	sindicato.setNome(rs.getString("nome"));
    	sindicato.setTaxa(rs.getFloat("taxa"));
                
        return sindicato;
    }
    
    public Integer gravar(Sindicato sindicato)
    {

        int retornoBanco = 0;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(
                        "INSERT INTO TB_SINDICATO (nome, " + 
                        "taxa) " +
                        " VALUES (?, ?)");
                pstmt.setString(1, sindicato.getNome());
                pstmt.setFloat(2, sindicato.getTaxa());

				retornoBanco = pstmt.executeUpdate();
            } catch (SQLException e) {
                System.out.println("Inclusao Falhou!!!\n" + e.getMessage());
            } finally {
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }

        return retornoBanco;
    }
    
	public List<Sindicato> lerTodos()
    {
    	Statement stmt = null;
        List<Sindicato> sindicatos = new ArrayList<Sindicato>();

        Connection conn = Conexao.getInstanciaUnica().criaConexao();
        
        if (conn != null) {
            try {
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(
                        "SELECT * FROM TB_SINDICATO");
                rs.last();
                rs.beforeFirst();
                while (rs.next()) {
                	sindicatos.add(carregaDados(rs));
                }
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return sindicatos;
    }

	public Sindicato ler(Integer idSindicato)
	{
		Statement stmt = null;
		Sindicato sindicato = null;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();
        
        if (conn != null) {
            try {
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(
                        "SELECT * FROM TB_SINDICATO WHERE idSindicato = " +
                        idSindicato);
                if (rs.next()) {
                	sindicato = carregaDados(rs);
                } else {
                	sindicato = null;
                }
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return sindicato;
	}
	
	public Integer remover(Integer idSindicato)
	{
		int n = 0;
        Statement stmt = null;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            try {
                stmt = conn.createStatement();
                n = stmt.executeUpdate(
                        "DELETE FROM TB_SINDICATO WHERE idSindicato = " +
                        idSindicato);
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return n;
	}
	
	public Integer alterar(Sindicato sindicato)
	{
		int n = 0;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(
                        "UPDATE TB_SINDICATO SET " +
                        "nome = ?, " + 
                        "taxa = ? " +
                        "WHERE idSindicato = ?");
                
                pstmt.setString(1, sindicato.getNome());
                pstmt.setFloat(2, sindicato.getTaxa());
                pstmt.setInt(3, sindicato.getIdSindicato());
                
                n = pstmt.executeUpdate();
                conn.close();
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return n;
	}
}
